package cn.siat.gene.controller;

import com.alibaba.fastjson.JSONObject;
import cn.siat.common.core.constant.HttpStatus;
import cn.siat.common.core.web.controller.BaseController;
import cn.siat.common.core.web.domain.AjaxResult;
import cn.siat.common.core.web.page.TableDataInfo;
import cn.siat.common.redis.service.RedisService;
import cn.siat.common.security.utils.SecurityUtils;
import cn.siat.gene.service.ComponentService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/component")
@RequiredArgsConstructor
public class ComponentController extends BaseController {

    private final  ComponentService componentService;
    private final RedisService redisService;

    /**
     * 从元件库获取Token
     * @return
     */
    @GetMapping("/user/getToken")
    public String getToken(){
        String token = componentService.getToken();
        return token;
    }

    @PutMapping("/uuid")
    public AjaxResult destroyUUID(String uuid){
        String redisKey = "Component_Flag:" + SecurityUtils.getUserId()+":"+uuid;
        System.err.println("uuid:"+uuid);
        return toAjax(redisService.deleteObject(redisKey));
    }

    /**
     *  验证元件库账号
     * @return
     */
    @PostMapping ("/user/verifyToken")
    public AjaxResult verifyToken(@RequestBody Map<String,String> map){
        String username = map.get("username");
        String password = map.get("password");
        JSONObject jsonObject = componentService.verifyToken(username, password);
        String msg = jsonObject.toJSONString();
        return jsonObject.getBoolean("success")?AjaxResult.success(msg):AjaxResult.error(msg);
    }

    /**
     * 获取常用列表
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo getList(String input,String type , Integer pageNum,Integer pageSize){
        TableDataInfo tableDataInfo = componentService.getList(input,type,pageNum,pageSize);
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }
    /**
     * 获取更多列表
     * @return
     */
    @GetMapping("/moreList")
    public TableDataInfo getMoreList(String input,String type , Integer pageNum,Integer pageSize){
        TableDataInfo tableDataInfo = componentService.getMoreList(input,type,pageNum,pageSize);
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }

//    /**
//     * 根据元件code获取序列
//     * @param code
//     * @param exportType 导出方式(csv、fasta、genbank、sbol、sbol2、genpept、gff3)
//     * @return
//     */
//    @GetMapping("/sequence/{code}/{exportType}")
//    public JSONObject getSequenceListByCode(@PathVariable String code, @PathVariable String exportType) {
//        return componentService.getSequenceListByCode(code, exportType);
//    }

    /**
     * 根据元件ID获取元件详情
     * @param code
     * @return
     */
    @GetMapping("/info/{code}/{flag}")
    public AjaxResult getComponentByCode(@PathVariable String code,@PathVariable  Integer flag) {
        return AjaxResult.success(componentService.getComponentByCode(code,flag));
    }

}
